<script setup>
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
const userStore = useUserStore()
const router = useRouter()
const formDate = ref({
  username: 'admin',
  password: '111111'
})
// 登录表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ]
}
// 表单ref
const formRef = ref(null)
// 登录提交
const onSubmit = () => {
  //验证表单
  formRef.value.validate(async (valid) => {
    if (valid) {
      try {
        await userStore.userLogin(formDate.value)
        ElNotification({
          title: '登录成功',
          message: '欢迎来到甄谷优选',
          duration: 2000,
          type: 'success',
          onClose: () => {
            router.push('/')
          }
        })
      } catch (error) {
        ElMessage.error(error.message)
      }


    } else {
      // 登录失败
      return false
    }
  })
}
</script>
<template>
  <div class="container-login">
    <div class="picture">
      <img src="@/assets/image/login_left.png" alt="">
    </div>
    <div class="login-form">
      <div class="title">登录</div>
      <el-form ref="formRef" :rules="rules" :model="formDate" label-width="auto" style="max-width: 600px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formDate.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="formDate.password" show-password />
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="primary" @click="onSubmit">
          登录<el-icon class="el-icon--right">
            <Promotion />
          </el-icon>
        </el-button>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.container-login {
  width: 100vw;
  height: 100vh;
  background-image: linear-gradient(to top, #dad4ec 0%, #dad4ec 1%, #f3e7e9 100%);
  padding: 20px;
  display: flex;
  align-items: center;
  justify-content: space-evenly;

  .picture {
    width: 1000px;
    height: 700px;

    img {
      width: 100%;
      height: 100%;
    }
  }

  .login-form {
    width: 500px;
    height: 300px;
    background-color: #fff;
    border-radius: 20px;
    opacity: 0.8;
    box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.2);
    padding: 20px;

    .title {
      font-size: 30px;
      text-align: center;
      padding: 20px;
      color: #333;
    }

  }
}
</style>